package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private AttrValueMapper attrValueMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;
    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private PaymentTypeMapper paymentTypeMapper;
    @Resource
    private LeaseTermMapper leaseTermMapper;
    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void saveOrUpdateroom(RoomSubmitVo roomSubmitVo) {
//      判断是增加还是修改
        boolean b = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);
//      修改操作
        if (b) {
// 1.根据 id 删除图片列表
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphQueryWrapper);

// 2.根据 id 删除属性信息列表
            LambdaQueryWrapper<RoomAttrValue> attrValueQueryWrapper = new LambdaQueryWrapper<>();
            attrValueQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(attrValueQueryWrapper);

// 3.根据 id 删除配套信息列表
            LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
            facilityQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(facilityQueryWrapper);

// 4.根据 id 删除标签信息列表
            LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
            labelQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(labelQueryWrapper);

// 5.根据 id 删除支付方式列表
            LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
            paymentQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(paymentQueryWrapper);

// 6.根据 id 删除可选租期列表
            LambdaQueryWrapper<RoomLeaseTerm> leaseTermQueryWrapper = new LambdaQueryWrapper<>();
            leaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(leaseTermQueryWrapper);

//          同时删除缓存(在全部删除之后)
            String key = RedisConstant.APP_ROOM_PREFIX+roomSubmitVo.getId();
            redisTemplate.delete(key);

//          7.批量增加房间图片
            List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
            if (!CollectionUtils.isEmpty(graphVoList)) {
                ArrayList<GraphInfo> graphInfos = new ArrayList<>();
                for (GraphVo graphVo : graphVoList) {
                    GraphInfo info = new GraphInfo();
                    info.setName(graphVo.getName());
                    info.setUrl(graphVo.getUrl());
                    info.setItemType(ItemType.ROOM);
                    info.setItemId(roomSubmitVo.getId());
                    graphInfos.add(info);
                }
                graphInfoService.saveBatch(graphInfos);
            }
//          8.批量增加属性信息列表
            List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
            ArrayList<RoomAttrValue> roomattrValues = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue attrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomattrValues.add(attrValue);
            }
            roomAttrValueService.saveBatch(roomattrValues);
//          9.批量增加配套信息列表
            List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
            ArrayList<RoomFacility> facilityInfos = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                facilityInfos.add(RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build());
            }
            roomFacilityService.saveBatch(facilityInfos);
//          10.批量增加标签信息列表
            List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
            if (!CollectionUtils.isEmpty(labelInfoIds)) {
                ArrayList<RoomLabel> roomLabels = new ArrayList<>();
                for (Long labelId : labelInfoIds) {
                    roomLabels.add(RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelId).build());
                }
                roomLabelService.saveBatch(roomLabels);
            }
//          11.保存新的paymentTypeList
            List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
            if (!CollectionUtils.isEmpty(paymentTypeIds)) {
                ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
                for (Long paymentTypeId : paymentTypeIds) {
                    RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                    roomPaymentTypeList.add(roomPaymentType);
                }
                roomPaymentTypeService.saveBatch(roomPaymentTypeList);
            }
//          12.保存新的leaseTermList
            List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
            if (!CollectionUtils.isEmpty(leaseTermIds)) {
                ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
                for (Long leaseTermId : leaseTermIds) {
                    RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                    roomLeaseTerms.add(roomLeaseTerm);
                }
                roomLeaseTermService.saveBatch(roomLeaseTerms);
            }
        }
    }

    @Override
    public IPage<RoomItemVo> querypageItem(Page<Object> page, RoomQueryVo queryVo) {
        IPage<RoomItemVo> result = roomInfoMapper.pageRoomItemByQuery(page,queryVo);
        return result;
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
//      1.根据id查询房间信息 这里的id是room_id(房间id)
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
//      2.根据id查询图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM,id);
//      3.根据id查询属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListattrValueById(id);
//      4.根据id查询配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
//      5.根据id查询标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
//      6.根据id查询支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
//      7.根据id查询可选租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
//      8.查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
//      9.组装数据
        RoomDetailVo adminRoomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, adminRoomDetailVo);

        adminRoomDetailVo.setApartmentInfo(apartmentInfo);
        adminRoomDetailVo.setGraphVoList(graphVoList);
        adminRoomDetailVo.setAttrValueVoList(attrValueVoList);
        adminRoomDetailVo.setFacilityInfoList(facilityInfoList);
        adminRoomDetailVo.setLabelInfoList(labelInfoList);
        adminRoomDetailVo.setPaymentTypeList(paymentTypeList);
        adminRoomDetailVo.setLeaseTermList(leaseTermList);

        return adminRoomDetailVo;
    }

    @Override
    public void removeALLById(Long id) {
// 0.根据 id 删除房间信息
        roomInfoMapper.deleteById(id);
// 1.根据 id 删除图片列表
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoService.remove(graphQueryWrapper);

// 2.根据 id 删除属性信息列表
        LambdaQueryWrapper<RoomAttrValue> attrValueQueryWrapper = new LambdaQueryWrapper<>();
        attrValueQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(attrValueQueryWrapper);

// 3.根据 id 删除配套信息列表
        LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(facilityQueryWrapper);

// 4.根据 id 删除标签信息列表
        LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(labelQueryWrapper);

// 5.根据 id 删除支付方式列表
        LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
        paymentQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(paymentQueryWrapper);

// 6.根据 id 删除可选租期列表
        LambdaQueryWrapper<RoomLeaseTerm> leaseTermQueryWrapper = new LambdaQueryWrapper<>();
        leaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(leaseTermQueryWrapper);

//      同时删除缓存(在全部删除之后)
        String key = RedisConstant.APP_ROOM_PREFIX+id;
        redisTemplate.delete(key);
    }
}




